import { defineApplicationConfig } from '@vben/vite-config';
import Inspector from 'vite-plugin-vue-inspector';
import { loadEnv } from 'vite';
import vue from '@vitejs/plugin-vue';

function getApi() {
  const env = loadEnv(process.env.NODE_ENV, process.cwd()); // 获取.env文件里定义的环境变量
  console.log('env is ', process.env.NODE_ENV);
  return env.VITE_HC_HTTP;
}

export default defineApplicationConfig({
  overrides: {
    base: '/hc-iot/',
    optimizeDeps: {
      include: [
        'echarts/core',
        'echarts/charts',
        'echarts/components',
        'echarts/renderers',
        'qrcode',
        '@iconify/iconify',
        'ant-design-vue/es/locale/zh_CN',
        'ant-design-vue/es/locale/en_US',
      ],
    },
    server: {
      port: 3005,
      proxy: {
        '/hc-api': {
          target: getApi(),
          changeOrigin: true,
          // ws: true,
          rewrite: (path) => path.replace(new RegExp(`^/hc-api`), 'api'),
          // rewrite: (path) => path.replace(new RegExp(`^/hc-api`), ''),
          // only https
          // secure: false
        },
        '/ws': {
          target: getApi(),
          changeOrigin: true,
          ws: true,
          // rewrite: (path) => path.replace(new RegExp(`^/hc-api`), 'api'),
          // only https
          // secure: false
        },
        '/basic-api': {
          target: 'http://localhost:3000',
          changeOrigin: true,
          ws: true,
          rewrite: (path) => path.replace(new RegExp(`^/basic-api`), ''),
          // only https
          // secure: false
        },
        '/upload': {
          target: 'http://localhost:3300/upload',
          changeOrigin: true,
          ws: true,
          rewrite: (path) => path.replace(new RegExp(`^/upload`), ''),
        },
      },
      open: true, // 项目启动后，自动打开
      warmup: {
        clientFiles: ['./index.html', './src/{views,components}/*'],
      },
    },
    plugins: [
      Inspector({
        openInEditorHost: 'http://localhost:5173',
      }),
      // vue({
      //   template: {
      //     compilerOptions: {
      //       isCustomElement: (tag) => /^micro-app/.test(tag),
      //     },
      //   },
      // }),
    ],
  },
});
